package cn.jiyun.controller;

import cn.jiyun.constant.MessageConstant;
import cn.jiyun.constant.RedisMessageConstant;
import cn.jiyun.entity.Result;
import cn.jiyun.utils.SMSUtils;
import cn.jiyun.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("validateCode")
public class ValidateCodeController {

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("send4Order")
    public Result send4Order(String telephone){
        try {
            String code = ValidateCodeUtils.generateValidateCode4String(6);
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,code);

            //存起来
            Jedis jedis = jedisPool.getResource();
            //指定人 -  指定业务模块（业务码）
            jedis.setex(RedisMessageConstant.SENDTYPE_ORDER+":"+telephone,300000000,code);


            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }

    @RequestMapping("send4Login")
    public Result send4Login(String telephone){
        try {
            String code = ValidateCodeUtils.generateValidateCode4String(6);
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,code);

            //存起来
            Jedis jedis = jedisPool.getResource();
            //指定人 -  指定业务模块（业务码）
            jedis.setex(RedisMessageConstant.SENDTYPE_LOGIN+":"+telephone,300000000,code);


            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }
}
